import { BrowserWindow, ipcMain, screen } from "electron";
import { uIOhook } from 'uiohook-napi';

export enum ResizeArea {
  TOP = 'top',
  TOP_RIGHT = 'top-right',
  RIGHT = 'right',
  BOTTOM_RIGHT = 'bottom-right',
  BOTTOM = 'bottom',
  BOTTOM_LEFT = 'bottom-left',
  LEFT = 'left',
  TOP_LEFT = 'top-left',
}

export function handleOffsetWindow():void {
  let resizeArea: ResizeArea | null = null;
  let resizeWindow: Electron.BrowserWindow | null = null;
  let startWindowInfo: { x: number, y: number, width: number, height: number } | null = null;
  let startMouseInfo: { x: number, y: number } | null = null;
  let mouseDownOnMoving: boolean = false;
  let datelX,datelY;
  uIOhook.on('mousemove', (e) => {
    if (mouseDownOnMoving && resizeArea && resizeWindow) {
      let updateInfo = {
        x: startWindowInfo!.x,
        y: startWindowInfo!.y,
        width: startWindowInfo!.width,
        height: startWindowInfo!.height,
      }
      datelX =  e.x - startMouseInfo?.x!
      datelY =  e.y - startMouseInfo?.y!
      if (resizeArea == ResizeArea.RIGHT) {
        updateInfo.width = startWindowInfo?.width! + datelX;
      } else if (resizeArea == ResizeArea.LEFT) {
        updateInfo.x = startWindowInfo?.x! + datelX;
        updateInfo.width = startWindowInfo?.width! - datelX;
      } else if (resizeArea == ResizeArea.TOP) {
        updateInfo.y = startWindowInfo?.y! + datelY;
        updateInfo.height = startWindowInfo?.height! - datelY;
      } else if (resizeArea == ResizeArea.BOTTOM) {
        updateInfo.height = startWindowInfo?.height! + datelY;
      } else if (resizeArea == ResizeArea.TOP_LEFT) {
        updateInfo.x = startWindowInfo?.x! + datelX;
        updateInfo.y = startWindowInfo?.y! + datelY;
        updateInfo.width = startWindowInfo?.width! - datelX;
        updateInfo.height = startWindowInfo?.height! - datelY;
      } else if (resizeArea == ResizeArea.TOP_RIGHT) {
        updateInfo.y = startWindowInfo?.y! + datelY;
        updateInfo.width = startWindowInfo?.width! + datelX;
        updateInfo.height = startWindowInfo?.height! - datelY;
      } else if (resizeArea == ResizeArea.BOTTOM_LEFT) {
        updateInfo.x = startWindowInfo?.x! + datelX;
        updateInfo.width = startWindowInfo?.width! - datelX;
        updateInfo.height = startWindowInfo?.height! + datelY;
      } else if (resizeArea == ResizeArea.BOTTOM_RIGHT) {
        updateInfo.width = startWindowInfo?.width! + datelX;
        updateInfo.height = startWindowInfo?.height! + datelY;
      }


      let width = Math.max(resizeWindow.getMinimumSize()[0],updateInfo.width)
      let height = Math.max(resizeWindow.getMinimumSize()[1],updateInfo.height)

      if (width == resizeWindow.getMinimumSize()[0]) {
        updateInfo.x = startWindowInfo?.x!
      }

      if (height == resizeWindow.getMinimumSize()[1]) {
        updateInfo.y = startWindowInfo?.y!
      }

      resizeWindow.setResizable(true)
      resizeWindow?.setBounds({
        x: updateInfo.x,
        y: updateInfo.y,
        width: width,
        height: height,
      }, false)
      resizeWindow.setResizable(false)
    }
  })
  uIOhook.on('mouseup', (event) => {
    console.log('松开了', event)
    if (event.button == 1 && mouseDownOnMoving) {
      mouseDownOnMoving = false
      resizeWindow = null;
    }
  })


  ipcMain.on('offset-window:mouse-down', (event, { area }: { area: ResizeArea }) => {
    mouseDownOnMoving = true;
    resizeArea = area;
    resizeWindow = BrowserWindow.fromWebContents(event.sender)!;
    startWindowInfo = {
      x: resizeWindow.getBounds().x,
      y: resizeWindow.getBounds().y,
      width: resizeWindow.getBounds().width,
      height: resizeWindow.getBounds().height,
    }
    startMouseInfo = {
      x: screen.getCursorScreenPoint().x,
      y: screen.getCursorScreenPoint().y,
    }
  })

  ipcMain.on('offset-window:mouse-up', () => {
    // 停止定时器
    mouseDownOnMoving = false
    resizeWindow = null;
  })


  uIOhook.start()
}
